package com.example.experiments;

import com.example.experiments.util.SoundPlayer;
import android.annotation.TargetApi;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.*;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/**
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 * 
 * @see SystemUiHider
 */
public class MainActivity extends Activity 
{

	public static final int USER_COLOR = Color.WHITE;
	public static final int CPU_COLOR = Color.LTGRAY;
	
	public static Activity instance;
	public static EditText console;
	public static EditText input;
	
	//Called when the Activity is created
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		//Remove title bar
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		instance = this;
		setContentView(R.layout.activity_main);
		console = (EditText) findViewById(R.id.consoleBox);
		input = (EditText) findViewById(R.id.inputBox);
		
		input.setOnEditorActionListener(new TextView.OnEditorActionListener()
		{
			@Override
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)
				{
					EditText input = (EditText) v;
					CharSequence inputLine = input.getText().toString().replace("\n", "");
					input.setText("");
					print(inputLine.toString(), USER_COLOR);
					//Sounds fail sadly
					//SoundPlayer.playSound(MainActivity.instance.getApplicationContext(), R.raw.highbeep);
					MainActivity.processCommand((String) inputLine);
				}
				return false;
			}
		});
	}
	
	//Prints a new line to the console with a given color
	public static void print(String str, int rgb)
	{
		Spannable spannable = new SpannableString(str);
		spannable.setSpan(new ForegroundColorSpan(rgb), 0, spannable.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
		console.append(spannable);
		console.append("\n");
	}
	
	//called when the user presses the enter key
	public static void processCommand(String input)
	{
		print("clever response to " + input, CPU_COLOR);
	}

}
